from PySide6.QtUiTools import QUiLoader
from PySide6.QtWidgets import QLineEdit, QMessageBox

from entity import currentuser
from opsql import studentOp


class Sself():
    def __init__(self):
        self.ui = QUiLoader().load('view/账户信息.ui')
        self.ui.moneyshow.setReadOnly(True)
        self.ui.idIn.setReadOnly(True)
        self.ui.passwordIn.setEchoMode(QLineEdit.Password)
        self.init()
        self.ui.modifyBtn.clicked.connect(self.modify)
        self.ui.moneyBtn.clicked.connect(self.money)

    def init(self):
        result=studentOp.searchself(currentuser.current_userid)
        self.ui.moneyshow.setText(str(result[1]))
        self.ui.nameIn.setText(str(result[4]))
        self.ui.phone.setText(str(result[3]))
        self.ui.idIn.setText(str(result[0]))

    def modify(self):
        id=self.ui.idIn.text()
        password=self.ui.passwordIn.text()
        phone=self.ui.phone.text()
        name=self.ui.nameIn.text()
        if studentOp.modifyself(id,password,phone,name):
            QMessageBox.information(self.ui,'修改成功','修改成功')
            self.init()
        else:
            QMessageBox.warning(self.ui,'修改失败','修改失败')

    def money(self):
        num=self.ui.moneyIn.text()
        sid=currentuser.current_userid
        if studentOp.addmoney(num,sid):
            QMessageBox.information(self.ui,'充值成功','充值成功')
            self.init()
        else:
            QMessageBox.warning(self.ui,'充值失败','充值失败')
